﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.Web;
using Hxsoft.AppV1.Module;
using Hxsoft.AppV1.WebUI;

namespace Hxsoft.AppV1._Controls
{
	public partial class ResourceQuickUploader : System.Web.UI.Page
	{
		protected override void OnLoad(EventArgs e)
		{
			// Get the posted file.
			HttpPostedFile oFile = Request.Files["NewFile"];

			// Check if the file has been correctly uploaded
			if (oFile == null || oFile.ContentLength == 0)
			{
				SendResults(202);
				return;
			}

			// 取得信息的全局标识
			Guid? globalID = null;
			if (Request.QueryString["GlobalID"] != null)
				globalID = new Guid(Request.QueryString["GlobalID"]);
			if (globalID == null)
			{
				SendResults(1, "", "", Res.ResourceUploadGlobalIdLose);
				return;
			}

			// 取得信息的类别
			string infoType = Request.QueryString["InfoType"];

			//验证文件类型
			string fullFileName = UploadRule.GetFileFullName(oFile.FileName);
			string fileName = UploadRule.GetFileName(fullFileName);
			string resourceType = UploadRule.GetFileType(fullFileName);
			if (!UploadRule.IsAllowUploadResourceType(resourceType))
			{
				SendResults(1, "", "", string.Format(Res.ResourceUploadTypeValidateErrorMessage, UploadRule.GetAllowUploadResourceType()));
				return;
			}

			//验证文件大小
			if (!UploadRule.IsAllowUploadResourceSize(oFile.InputStream.Length))
			{
				SendResults(1, "", "", string.Format(Res.ResourceUploadMaxSizeValidateErrorMessage, UploadRule.GetUploadResourceMaxSize()));
				return;
			}

			InfoResource resource = new InfoResource();
			resource.GlobalId = globalID.Value;
			resource.ResourceType = resourceType;
			resource.Name = fileName;
			resource.Content = new System.Data.Linq.Binary(oFile.GetFileBytes());

			if (!InfoResources.GetInsertRight(resource))
			{
				SendResults(1, "", "", Res.PermissionExceptionMessage);
				return;
			}


			// 保存资源
			resource = InfoResources.Insert(resource, infoType, new InfoResourceWatermark() { HasWatermark = true });

			string sFileUrl = GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl(resource.ResourceId, null, resourceType);
			SendResults(0, sFileUrl, "");
		}


		#region SendResults Method

		private void SendResults(int errorNumber)
		{
			SendResults(errorNumber, "", "", "");
		}

		private void SendResults(int errorNumber, string fileUrl, string fileName)
		{
			SendResults(errorNumber, fileUrl, fileName, "");
		}

		private void SendResults(int errorNumber, string fileUrl, string fileName, string customMsg)
		{
			Response.Clear();

			Response.Write("<script type=\"text/javascript\">");
			Response.Write("window.parent.OnUploadCompleted(" + errorNumber + ",'" + fileUrl.Replace("'", "\\'") + "','" + fileName.Replace("'", "\\'") + "','" + customMsg.Replace("'", "\\'") + "') ;");
			Response.Write("</script>");

			Response.End();
		}

		#endregion
	}
}